package server.controller.seal;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import server.db.primary.mapper.map.SealAppMapper;
import server.db.primary.model.map.AfficheApp;
import server.db.primary.model.map.SealApp;
import server.tool.FileTool;
import server.tool.Res;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class SealAppController {

    @Autowired
    SealAppMapper sealAppMapper;

    /**
     * 根据流水号查询
     * @param numberId
     * @return
     */
    @RequestMapping("/sealApp/sealAppBynumberId")
    public Res sealAppBynumberId(Long numberId) {
        List<SealApp> sealApps = sealAppMapper.selectByNumberId(numberId);
        return Res.success(sealApps);
    }


    /**
     * 插入附件数据
     * @param record sealApp附件对象
     * @return 成功行数
     */
    @RequestMapping("/sealApp/Insert")
    public Res sealByInsert(SealApp record) {
        int insert = sealAppMapper.insert(record);
        return Res.success(insert);
    }

    /**
     * 根据sealApp主键删除对应的数据
     * @param id sealApp主键
     * @return 成功1  失败0
     */
    @RequestMapping("/sealApp/Delete")
    public Res sealAppByDelete(Long id) {
        int count = sealAppMapper.deleteByPrimaryKey(id);
        return Res.success(count);
    }

    /**
     * 插入附件数据
     * @param record sealApp附件对象
     * @return 成功行数
     */
    @RequestMapping("/sealApp/InsertFile")
    public Res sealByInsertFile(SealApp record,MultipartFile file, String path) {
        JSONObject data= FileTool.fileUpload(file,path);

        String img_path = data.getString("data");
        System.out.println(img_path);

        //获取文件扩展名，并赋值
        String fileName = file.getOriginalFilename();
        String extensionName = fileName.substring(fileName.lastIndexOf("."));
        record.setAppName(fileName);

        //添加路径
        record.setData(img_path);
        int insert = sealAppMapper.insert(record);
        return Res.success(insert);
    }


    @RequestMapping("/sealApp/upload")
    public Res fileupload(SealApp sealApp, MultipartFile file, HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();

        String originalFilename = file.getOriginalFilename();
        String substring1 = originalFilename.substring(originalFilename.lastIndexOf("."));
        sealApp.setAppName(originalFilename);

        //获取文件的后缀名
        String substring2 = originalFilename.substring(originalFilename.indexOf("."));

        if (!originalFilename.endsWith(substring2)) {
            return Res.failure(null,"文件格式错误");
        }
        String realPath = request.getServletContext().getRealPath("/");
        String substring = realPath.substring(0, realPath.lastIndexOf("\\"));

        File folder = new File(realPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String newName = UUID.randomUUID() + substring2;

        try {
            file.transferTo(new File(folder, newName));
            String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + newName;
            //存localhost
            sealApp.setData(url);
            //路径

            result.put("status", "success");
            result.put("message", "上传成功");
            result.put("url", url);
        } catch (IOException e) {
            result.put("status", "error");
            result.put("messgae", e.getMessage());
        }
        int insert = sealAppMapper.insert(sealApp);
        System.out.println("影响行数---->"+insert);
        return Res.success(result);
    }
}
